import UIKit

/*:
 求数组中最大子序列和
 */
var numbers = [-2,1,-3,4,-1,2,1,-5,4]

func maxSeqence3(_ arr: [Int]) -> Int {
    var max = 0
    var temp = 0
    for i in 0..<numbers.count {
        temp += arr[i]
        if max < temp {
            max = temp
        }
        if temp <= 0 {
            temp = 0
        }
        
    }
    return max
}
print(maxSeqence3(numbers))

func maxSeqence2(_ arr: [Int]) -> Int {
    
    var max = 0
    for i in 0..<arr.count {
        var temp = arr[i]
        for j in i+1..<arr.count {
            temp += arr[j]
            if max < temp {
                max = temp
            }
        }
    }
    
    return max
}
print(maxSeqence3(numbers))
